package com.example.demoredisspringboot.service.impl;

import com.example.demoredisspringboot.pojo.User;
import com.example.demoredisspringboot.service.UserService;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <b><code>UserServiceImpl</code></b>
 * <p>
 * Description
 * <p>
 * <b>Create Time:</b> 2022/4/24 9:06.
 *
 * @author qd.yt
 * @version 0.0.1
 * @since demo-redis-springboot v0.0.1
 */
@Service
public class UserServiceImpl implements UserService {
    @Override
    @Cacheable(cacheNames="user",sync=true)
    public User findById(String id) {
        User user =  findByIdDao(id);
        if (user != null){
            System.out.println("menu.name = " + user.getName());
        }
        return user;
    }

    @Override
    @Cacheable("userList")
    public List<User> findByIds(Integer count) {
        List<User> userList = getUserList(count);
        if (userList != null){
            System.out.println("user count = " + userList.size());
        }
        return userList;
    }

    private List<User> getUserList(Integer count) {
        System.out.println("查询数据库.....");
        List<User> userList = new ArrayList<>();
        for(int i=0;i<count;i++){
            userList.add(new User("name"+i,""+i,i+"22"));
        }
        return userList;
    }

    public User findByIdDao(String id){
        System.out.println("查询数据库.....");
        if(id.equals("1")){
            return new User("name","1","123");
        }else {
            return null;
        }
    }
}
